#include <fs.h>
#include <ipc.h>

typedef struct tar_fhead_st
{
	unsigned char name[100];
	unsigned char mode[8];
	unsigned char uid[8];
	unsigned char gid[8];
	unsigned char size[12];
	unsigned char mtime[12];
	unsigned char checksum[8];
	unsigned char linkflag[1];
	unsigned char linkname[100];
	unsigned char pad[255];
}__attribute__((packed)) tar_head;

unsigned int elf_size(unsigned char *size)
{
	unsigned int ret = 0;
	unsigned int i;
	for(i=0; i<12 && size[i]; i++)
	{
		ret = ret*8;
		ret = ret+((unsigned int)size[i] - (unsigned int)'0');
	}
	return ret;
}

int main(unsigned int arg)
{
	printf("\nInit daemon loaded!\n");
	printf("\nDrivers at %x", arg);
	unsigned int i = 0;
	tar_head *head = (tar_head *)arg;
	unsigned int pr;
	while(head->name[0])
	{
		printf("\n Loading: ");
		printf(head->name);
		i = fork();
		if(i==0)
		{
			printf(" %x ", getpid());
			exec((unsigned int *)((unsigned int)head + 512),0);
		}
		else
		{
			head = (tar_head *)((unsigned int)head + 512 + elf_size(head->size) + (512-elf_size(head->size)%512) );
		}
	}
	
	unsigned char *buffer = (unsigned char *)malloc(sizeof(unsigned char)*1000);
	FILE myfile;
	myfile.server = 0x4;
//	read_fs(&myfile,0,1000, buffer);
//	printf("Readed \n");
//	write_fs(&myfile,0,1000,buffer);
//	printf(buffer);
	
	done();
}
